<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
/***************************
傳進參數
GambleID：操盤序號
Bet_Type：下注種類

OddsID：賠率序號
Odds：賠率值
Number：號碼

LottoID：樂透種類序號
StakeMoney：下注金額
CheckKey：檢查鍵值(防止下注重複)
***************************/
include_once("../config.inc.php");

session_start();
$_LangPath = "joinnumbercheckoutinsert";	//語言檔參數，務必放在最前面
$objMemberInfo = $_SESSION["Member_Info"];

if($objMemberInfo -> SuppleID == "" && $objMemberInfo -> WagerType == 0)include_once(__Member_Path."/logincheck.php");
include(__Language_Path."/selectlanguage_member.php");
include_once(__Modules_Path."/Wager.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Common_Path."/Lotto.inc.php");
include_once(__Common_Path."/ExchangeRate.inc.php");
include_once(__Modules_Path."/LogRecord.lib.php");
include_once(__Modules_Path."/joinnumber.lib.php");
if($objMemberInfo -> isLock == '2'){
	$GLOBALS["errormessage"] = $_strStopWager;
	include("wager_error.php");
}

//Start Time
$startTime = Getmicrotime ();
$intLID = (int)$_data["LottoID"];
$intGID = (int)$_data["GambleID"];
$intLType = (int)$_arrLotto[$intLID]["LType"];
$intBT = (int)$_data["Bet_Type"];
$intNumber = (int)$_data["Number"];
if($intNumber != 1)$intNumber = 0;
//找出 Apoint
$intAN = (int)$_arrGLineNumToApointNum[$intLID][$intBT][$intNumber];

$intStakeMoney = (int)$_data["StakeMoney"];
$intNum 	= '0';
$_data["JoinNumber"] = trim($_data["JoinNumber"]);
$_data["OddsID"] = trim($_data["OddsID"]);
$_data["Odds"] = trim($_data["Odds"]);
$_data["CheckKey1"] = trim($_data["CheckKey1"]);
$_data["CheckKey2"] = trim($_data["CheckKey2"]);

if(__isConnectToOtherWeb == 1){
	$flMemERate = (empty($objMemberInfo -> ExRate))?1:$objMemberInfo -> ExRate;
}else{
	$flMemERate = (empty($_ECRate[$objMemberInfo -> ERID]["Rate"]))?1:$_ECRate[$objMemberInfo -> ERID]["Rate"];
}
$GroupCounter = 2;			//預設2個號碼為一組
$lineFeedCount = 6;
$JoinArray = null;
$PayOffMoney = null;
$PayOffMoney_second = null;
$JoinNumberList = null;
$JoinNumberValue = null;

//設定多少為一組 + 組合成功顯示可贏金額字串
switch ($intBT) {
	case 7:
		//三全中
		$PayOffValue	= $_strJoinNumberCheckOutInsert[AllOfThree] .'&nbsp;<font class=left_jn_pg_odds>$flOdds</font>'. $_strJoinNumberCheckOutInsert[AColon] .'$PayOffMoney';
		$GroupCounter	= 3;	// 3個號碼為一組
		break;
	case 8:
		//三中二
		$PayOffValue	= $_strJoinNumberCheckOutInsert[TwoOfThree] .'<br>'. $_strJoinNumberCheckOutInsert[WinTwo] .'&nbsp;<font class=left_jn_pg_odds>$flOdds</font>'. $_strJoinNumberCheckOutInsert[AColon] .'$PayOffMoney<br>'. $_strJoinNumberCheckOutInsert[WinThree] .'&nbsp;<font class=left_jn_pg_odds>$flOdds_second</font>'. $_strJoinNumberCheckOutInsert[AColon] .'$PayOffMoney_second';
		$GroupCounter	= 3;	// 3個號碼為一組
		break;
	case 9:
		//二全中
		$PayOffValue	= $_strJoinNumberCheckOutInsert[AllOfTwo] .'&nbsp;<font class=left_jn_pg_odds>$flOdds</font>'. $_strJoinNumberCheckOutInsert[AColon] .'$PayOffMoney';
		break;
	case 10:
		//二中特
		$PayOffValue	= $_strJoinNumberCheckOutInsert[SpecialOfTwo] .'<br>'. $_strJoinNumberCheckOutInsert[WinSpecial] .'&nbsp;<font class=left_jn_pg_odds>$flOdds</font>'. $_strJoinNumberCheckOutInsert[AColon] .'$PayOffMoney<br>'. $_strJoinNumberCheckOutInsert[WinTwo] .'&nbsp;<font class=left_jn_pg_odds>$flOdds_second</font>'. $_strJoinNumberCheckOutInsert[AColon] .'$PayOffMoney_second';
		break;
	case 11:
		//特串
		$PayOffValue	= $_strJoinNumberCheckOutInsert[SpecialLink] .'&nbsp;<font class=left_jn_pg_odds>$flOdds</font>'. $_strJoinNumberCheckOutInsert[AColon] .'$PayOffMoney';
		break;

	default:
		$Err_Title = $_strJoinNumberCheckOutInsert[BetError];
		$Err_Message = $_strJoinNumberCheckOutInsert[BetAgain];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
		break;
}

if($intNumber == 0){
	//將 JoinNumber 切開後指給 JoinNumber 陣列
	$arrAllJNumber 	= explode('|', $_data[JoinNumber]);
	array_unshift($arrAllJNumber, "NOUSE");
	unset ($arrAllJNumber[0]);
	$arrTmp = array();
	foreach($arrAllJNumber as $k => $v){
		$intN = (int)$v;
		if($intN < 1 || $intN > 49){
			$Err_Title = $_strJoinNumberCheckOutInsert[BetError];
			$Err_Message = $_strJoinNumberCheckOutInsert[BetAgain];
			$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
			include("wager_error.php");
		}
		$arrAllJNumber[$k] = sprintf("%02s", $intN);
		$arrTmp[$intN] = $intN;
	}
	if(sizeof($arrAllJNumber) > 10 || sizeof($arrAllJNumber) != sizeof($arrTmp)){
		$Err_Title = $_strJoinNumberCheckOutInsert[BetError];
		$Err_Message = $_strJoinNumberCheckOutInsert[BetAgain];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
	//取得連碼陣列
	$JoinArray = combination($arrAllJNumber, $GroupCounter);
	//組合顯示下注號碼
	foreach ($arrAllJNumber as $value) {
		$ChoiceCounter++;

		if ($ChoiceCounter >= $lineFeedCount && $ChoiceCounter % $lineFeedCount == 0) {
			$ChoiceNumberList .= $value . '<br>';
		} else {
			$ChoiceNumberList .= $value . ", ";//$_strJoinNumberCheckOutInsert[OffItemsSeries];
		}
	}
	if (substr($ChoiceNumberList, -1) == '>') {
		$ChoiceNumberList = substr($ChoiceNumberList, 0, -4);
	} else {
		$ChoiceNumberList = substr($ChoiceNumberList, 0, -2);
	}
	$strJoinNumber = join(",", $arrAllJNumber);

//排碰
}else{
	$arrTip = array("a", "b", "c");
	$arrAllJNumber 	= explode('|', $_data[JoinNumber]);

	$arrCTNo = array();
	$arrChoiceTNo = array();
	$arrChoiceList = array();
	$arrOrderList = array();
	$arrChoice = array();
	for($i = 0; $i < $GroupCounter; $i++){
		$arrTmp = explode(",", $arrAllJNumber[$i]);
		foreach($arrTmp as $strN){
			$intN = (int)$strN;
			if($intN < 1 || $intN > 49){
				$Err_Title = $_strJoinNumberCheckOutInsert[BetError];
				$Err_Message = $_strJoinNumberCheckOutInsert[BetAgain];
				$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
				include("wager_error.php");
			}
			$arrCTNo[$intN] = $i;
		}
	}
	foreach($arrCTNo as $intN => $intCNo){
		$arrChoiceTNo[$intCNo][] = sprintf("%02s", $intN);
	}
	for($i = 0; $i < $GroupCounter; $i++){
		if(!is_array($arrChoiceTNo[$i])){
			$Err_Title = $_strJoinNumberCheckOutInsert[BetError];
			$Err_Message = $_strJoinNumberCheckOutInsert[BetAgain];
			include("wager_error.php");

		}elseif(sizeof($arrChoiceTNo[$i]) > 12){
			$Err_Title = $_strJoinNumberCheckOutInsert[BetError];
			$Err_Message = $_strJoinNumberCheckOutInsert[BetAgain];
			include("wager_error.php");
		}
		$arrChoiceList[$i] = '[<font class="left_jn_pg_choice_no">'.join(",", $arrChoiceTNo[$i]). '</font>]';
		$arrOrderList[$i] = join(",", $arrChoiceTNo[$i]);
		$arrChoice[$i] = $arrTip[$i].join(",".$arrTip[$i], $arrChoiceTNo[$i]);
	}
	$ChoiceNumberList = implode(" x <br>", $arrChoiceList);
	$OrderStakeNumber = implode("|", $arrOrderList);
	$strJoinNumber = implode("x", $arrChoice);
	//取得下注總組數
	$JoinArray = combinationForLine($arrChoiceTNo, $GroupCounter);
}

$arrJ_Number = array();
foreach ($JoinArray as $key => $value) {
	$arrJN	= explode('.', $value);
	sort($arrJN);
	$JoinArray[$key] = implode('.', $arrJN);
	//$arrJ_Number['J'.sprintf("%06d", implode('', $arrJN))] = 1;
	$strJ_Number = 'J'.sprintf("%012s", implode('', $arrJN));
	/*if(sizeof($arrJN) == 2){
		$strJ_Number = 'J'.sprintf("%04d", implode('', $arrJN));
	}else{
		$strJ_Number = 'J'.sprintf("%06d", implode('', $arrJN));
	}*/
	$arrJ_Number[$strJ_Number] = 1;
}

//取得連碼組數
$ChoiceGroup = count($JoinArray);

//將 OddsID 切開後指給 OddsID 陣列
$arrOddsID = explode('|', $_data["OddsID"]);

//將 Odds 切開後指給 Odds 陣列
$arrOdds = explode('|', $_data["Odds"]);

//設定總金額 - 金額乘組數
$_data["TotalStakeMoney"] 	= $intStakeMoney * $ChoiceGroup;

//檢查關鍵參數是否為空值
if(!$intLID || !$intLType || empty($intBT) || strlen($_data["CheckKey2"]) == 0 || strlen($_data["CheckKey1"]) == 0 || strlen($_data["OddsID"]) == 0 || empty($_data["StakeMoney"]) || empty($intGID) || !is_array($JoinArray)){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["BetAgain"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

//檢查下注金額是否為數值
/*if(!preg_match("/^[0-9]{0,7}$/", $intStakeMoney)){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["StakeisString"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}*/

//不正確下注模式
if(!($intBT >= 7 && $intBT <=11)){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["ErrBetType"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

//檢查 Bet_Type 為 8,10 時檢查傳入值是否有兩個賠率
if ($intBT == 8 && empty($arrOddsID[1])) {
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["ErrBetTypeSecond"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
} elseif ($intBT == 10 && empty($arrOddsID[1])) {
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["ErrBetTypeSecond"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

//會員、代理商、總代理、股東退水質不得為空值
foreach($objMemberInfo -> arrCommission as $intGroup => $arrC){
	$arrComm[$intGroup] = $arrC[$intLID][$intBT];
	if( empty($arrComm[$intGroup]) ){
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
		$Err_Message = $_strJoinNumberCheckOutInsert["CommissionErr"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
}

include_once(__Common_Path."/limit/LottoLimit_".$intLID.".inc.php");
include_once(__Common_Path."/betparam/BetParam_".$intLID.".inc.php");

//移除 once 後面需要重新載入
$_arrMemStake = getWagerArrayGoldByDB($objMemberInfo -> ID, $objMemberInfo -> WagerType);
if($_arrMemStake === false){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strJoinNumberCheckOutInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}
//include(__Root_Path."/tmp/credit-".$objMemberInfo -> ID.".php");

if(!is_array($_arrBetParam[$intBT][$intAN])){
	$Err_Message = "No Wager Relation Data!!";
	include("wager_error.php");
}
//===========================================================================//
//判斷最小單注或單場限額
$BetLimit = $objMemberInfo -> arrBetLimit[$intLID][$intBT];
$EventLimit = $objMemberInfo -> arrEventLimit[$intLID][$intBT];

if($_arrLottoLimit[$intBT][0] > 0){
	$_arrLottoLimit[$intBT][0] = floor($_arrLottoLimit[$intBT][0] / $flMemERate);
	if($_arrLottoLimit[$intBT][0] < $EventLimit)$EventLimit = $_arrLottoLimit[$intBT][0];
}

if($_arrLottoLimit[$intBT][1] > 0){
	$_arrLottoLimit[$intBT][1] = floor($_arrLottoLimit[$intBT][1] / $flMemERate);
	if($_arrLottoLimit[$intBT][1] < $BetLimit)$BetLimit = $_arrLottoLimit[$intBT][1];
}

//公司單注限額
$Com_BetLimit = floor($_arrBetParam[$intBT][$intAN]["Com_BetLimit"] / ($objMemberInfo -> DirectorScale*$flMemERate));
if($Com_BetLimit < $BetLimit)$BetLimit = $Com_BetLimit;

$intMinStake = floor($_arrBetParam[$intBT][$intAN]["MinStake"] / $flMemERate);
//下注金額不能小於最小下注金額
if($intStakeMoney < $intMinStake){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["NotUnderMinStake"]."(".number_format($intMinStake).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//下注金額不能大於單注限額
if($intStakeMoney > $BetLimit){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["NotOverMaxStake"]."(".number_format($BetLimit).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
/*下注總金額不能大於單場限額的剩餘額度
$intEvent = $_arrMemStake[$intBT][$intGID][$intNum];
if($_data["TotalStakeMoney"] > ($EventLimit - $intEvent)){
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["NotOverEvenLimit"];
	$Err_Message .= $_strJoinNumberCheckOutInsert["EvenLimit"]."(".number_format($EventLimit).")<br>";
	$Err_Message .= $_strJoinNumberCheckOutInsert["ThisStake"]."(".number_format($intEvent).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
*/
//下注總金額不能大於信額額度
$intEvent = $_arrMemStake[$intBT][$intGID]["_SUM"];
$intSum = ($objMemberInfo -> WagerType == 0)?$_arrMemStake["_SUM"]:$intEvent;
if($_data["TotalStakeMoney"] > ($objMemberInfo -> CreditLine - $intSum)){
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
	$Err_Message = $_strJoinNumberCheckOutInsert["NotOverCredit"];
	$Err_Message .= $_strJoinNumberCheckOutInsert["MemCredit"]."(".number_format($objMemberInfo -> CreditLine).")<br>";
	$Err_Message .= $_strJoinNumberCheckOutInsert["UsedCredit"]."(".number_format($intSum).")";
	$Err_Message .= "(".number_format($_data["TotalStakeMoney"]).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//下注金額不能大於上層信額額度
if($objMemberInfo -> WagerType == 0){
	if(isOverParentCredit($_data["TotalStakeMoney"]*$flMemERate, $objMemberInfo -> SubID, $objMemberInfo -> SubCreditLine, $objMemberInfo -> MainID, $objMemberInfo -> MainCreditLine)){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		if($GLOBALS["errormessage"] == "")$Err_Message = $_strJoinNumberCheckOutInsert["NotOverCredit"];
		$Err_Title = $_strJoinNumberCheckOutInsert["BetError"];
		$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
}

//剩餘信用額度
$CreditSurplus	= $objMemberInfo->CreditLine - $_arrMemStake["_SUM"];

//===========================================================================//
//include_once(__Modules_Path."/RealBet.lib.php");
//是否上層停押
//$strParentID = $objMemberInfo -> SubID.",".$objMemberInfo -> MainID.",".$objMemberInfo -> StockID;
//if(!$strTop = GetOneStopWagerBet($intGID, $intBT, $intNum, $strParentID)){
//	$Err_Message = $GLOBALS["errormessage"];
//	$Err_HiddenMessage = $GLOBALS["errorhidden"];
//	include("wager_error.php");
//}
//if($strTop == "Stop"){
//	$Err_Message = $_strJoinNumberCheckOutInsert["StopWager"];
//	include("wager_error.php");
//}

//===========================================================================//
//判斷是否啟動自動向上補貨
$isAutoSupple = false;
$arrAutoSuppleLimit = $objMemberInfo -> arrAutoSuppleLimit[$intLID][$intBT];
if(($objMemberInfo -> SubIsAutoSupple || $objMemberInfo -> MainIsAutoSupple || $objMemberInfo -> StockIsAutoSupple || $objMemberInfo -> DirectorIsAutoSupple || $objMemberInfo -> Layer6IsAutoSupple)){//$intLID == 1 && 
	if($_arrBetParam[$intBT][$intAN]["isAutoSupple"])$isAutoSupple = true;
}
if( ($arrAutoSuppleLimit[$objMemberInfo -> SubID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> SubID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> MainID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> MainID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> StockID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> StockID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> DirectorID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> DirectorID] == null) && 
	($arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] == null)
)$isAutoSupple = false;

$arrAutoSQL = array();
$arrRealStake = array();
$arrLayerID = array(2 => $objMemberInfo -> SubID, 3 => $objMemberInfo -> MainID, 4 => $objMemberInfo -> StockID, 5 => $objMemberInfo -> DirectorID, 5 => $objMemberInfo -> Layer6ID);
if($_arrBetParam[$intBT][$intAN]["isAutoSupple"]){
	$strJNumber = implode(",", $JoinArray);
	$strJNumber = str_replace('.', '', $strJNumber);
	//代理商
	if($objMemberInfo -> SubIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> SubID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> SubID] == null))$arrAutoSQL[] = "(select a.SubID as UserID, b.Number, SUM(IF(a.WagerType = 2, -1, 1*(1 - a.SubRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.SubID = ".$objMemberInfo -> SubID." and b.Number in (".$strJNumber.")  and a.Bet_Type = ".$intBT." group by b.Number)";

	//總代理
	if($objMemberInfo -> MainIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> MainID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> MainID] == null))$arrAutoSQL[] = "(select a.MainID as UserID, b.Number, SUM(IF(a.WagerType = 3, -1, 1*(a.SubRate - a.MainRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.MainID = ".$objMemberInfo -> MainID." and b.Number in (".$strJNumber.") and a.Bet_Type = ".$intBT." group by b.Number)";

	//股東
	if($objMemberInfo -> StockIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> StockID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> StockID] == null))$arrAutoSQL[] = "(select a.StockID as UserID, b.Number, SUM(IF(a.WagerType = 4, -1, 1*(a.MainRate - a.StockRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.StockID = ".$objMemberInfo -> StockID." and b.Number in (".$strJNumber.") and a.Bet_Type = ".$intBT." group by b.Number)";

	//總監
	if($objMemberInfo -> DirectorIsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> DirectorID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> DirectorID] == null))$arrAutoSQL[] = "(select a.DirectorID as UserID, b.Number, SUM(IF(a.WagerType = 5, -1, 1*(a.StockRate - a.DirectorRate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.DirectorID = ".$objMemberInfo -> DirectorID." and b.Number in (".$strJNumber.") and a.Bet_Type = ".$intBT." group by b.Number)";

	//業務總監
	if($objMemberInfo -> Layer6IsAutoSupple && !($arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] < 0 || $arrAutoSuppleLimit[$objMemberInfo -> Layer6ID] == null))$arrAutoSQL[] = "(select a.Layer6ID as UserID, b.Number, SUM(IF(a.WagerType = 6, -1, 1*(a.DirectorRate - a.Layer6Rate))*b.MemStake) as RealStake from `bet_BetListTmp` a, `bet_ParlaysNumberEven` b where a.GambleID = ".$intGID." and a.ID = b.BetListID and a.Layer6ID = ".$objMemberInfo -> Layer6ID." and b.Number in (".$strJNumber.") and a.Bet_Type = ".$intBT." group by b.Number)";
}
//print_r($arrAutoSQL);
//exit();
//交易開始
PMA_mysql_query("BEGIN", $_ConnMaster);

$arrAutoSuppleGroup = array();
$arrRealStake = array();
if(sizeof($arrAutoSQL) > 0){
	$strAutoSQL = implode(" union ", $arrAutoSQL);
//print $strAutoSQL;
	if(!$RS = PMA_mysql_query($strAutoSQL, $$_Conn)){
		$Err_Message = "Find Data Error!!";
		include("wager_error.php");
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$strNumber = 'J'.sprintf("%012s", $Rows -> Number);
		/*if(strlen($Rows -> Number) == 5 || strlen($Rows -> Number) == 3){
			$strNumber = 'J0'.$Rows -> Number;
		}else{
			$strNumber = 'J'.$Rows -> Number;
		}*/
		$arrRealStake[$strNumber][$Rows -> UserID] += $Rows -> RealStake;
		$arrJ_Number[$strNumber] = 1;
	}

	foreach($arrJ_Number as $strNumber => $v){
		foreach($arrLayerID as $intGroup => $intUID){
			$flRealStake = 0;
			$isLayerAutoSupple = false;
			$arrAutoSuppleGroupGold[$strNumber][$intGroup] = 0; //各階層自動補貨金額
			switch($intGroup){
				case 2:
					$flRealStake = $flMemERate*$intStakeMoney*(1 - $objMemberInfo -> SubScale);
					$isLayerAutoSupple = $objMemberInfo -> SubIsAutoSupple;
					break;

				case 3:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);
					$flRealStake += $arrAutoSuppleGroupGold[$strNumber][2]*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale);

					$isLayerAutoSupple = $objMemberInfo -> MainIsAutoSupple;
					break;

				case 4:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
					//print $flRealStake."\n";
					$flRealStake += ($arrAutoSuppleGroupGold[$strNumber][2] + $arrAutoSuppleGroupGold[$strNumber][3])*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale);
					//print $flRealStake."\n";

					$isLayerAutoSupple = $objMemberInfo -> StockIsAutoSupple;
					break;

				case 5:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
					//$flRealStake += ($arrAutoSuppleGroupGold[$strNumber][2] + $arrAutoSuppleGroupGold[$strNumber][3] + $arrAutoSuppleGroupGold[$strNumber][4])*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale);
					$flRealStake += $arrAutoSuppleGroupGold[$strNumber][2]*($objMemberInfo -> LayerScale[2][4] - $objMemberInfo -> LayerScale[2][5]) + $arrAutoSuppleGroupGold[$strNumber][3]*($objMemberInfo -> LayerScale[3][4] - $objMemberInfo -> LayerScale[3][5]) + $arrAutoSuppleGroupGold[$strNumber][4]*($objMemberInfo -> LayerScale[4][4] - $objMemberInfo -> LayerScale[4][5]);

					$isLayerAutoSupple = $objMemberInfo -> DirectorIsAutoSupple;
					break;

				case 6:
					$flRealStake = $flMemERate*$intStakeMoney*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale);
					$flRealStake += $arrAutoSuppleGroupGold[$strNumber][2]*($objMemberInfo -> LayerScale[2][5] - $objMemberInfo -> LayerScale[2][6]) + $arrAutoSuppleGroupGold[$strNumber][3]*($objMemberInfo -> LayerScale[3][5] - $objMemberInfo -> LayerScale[3][6]) + $arrAutoSuppleGroupGold[$strNumber][4]*($objMemberInfo -> LayerScale[4][5] - $objMemberInfo -> LayerScale[4][6] + $arrAutoSuppleGroupGold[$strNumber][5]*($objMemberInfo -> LayerScale[5][5] - $objMemberInfo -> LayerScale[5][6]);

					$isLayerAutoSupple = $objMemberInfo -> Layer6IsAutoSupple;
					break;
			}

			if($isLayerAutoSupple){
				if($arrAutoSuppleLimit[$intUID] < 0 || $arrAutoSuppleLimit[$intUID] == null)continue;

				if(($_arrBetParam[$intBT][$intAN]["AutoSuppleOverGold"] + $arrAutoSuppleLimit[$intUID]) <= ($arrRealStake[$strNumber][$intUID] + $flRealStake)){
					$arrAutoSuppleGold[$strNumber][$intUID] = $arrRealStake[$strNumber][$intUID] + $flRealStake - $arrAutoSuppleLimit[$intUID];
					$arrAutoSuppleGroup[$strNumber][$intUID] = $intGroup;
					$arrAutoSuppleGroupGold[$strNumber][$intGroup] = $arrAutoSuppleGold[$strNumber][$intUID];
				}
			}
		}
	}
}
//print_r($arrRealStake);
//print_r($arrAutoSuppleGold);
//print_r($arrAutoSuppleLimit);
//print_r($arrAutoSuppleGroup);
//print_r($arrAutoSuppleGroupGold);
//exit();
$arrCommission = array();
if($isAutoSupple && is_array($arrAutoSuppleGroup)){
	if(sizeof($arrAutoSuppleGroup) > 0){
		//找出代理商、總代理、股東、總監 A盤退水
		foreach($objMemberInfo -> arrCommissionA as $intUID => $arrC){
			$arrCommission[$intUID] = 1 - ($arrC[$intLID][$intBT] / 100);
		}
	}
}


if ($arrOddsID[1]) {
	//取得最新號碼賠率資料 (2個賠率)
	$arrGDate = getWagerArrayData($intLID, $intLType, $intGID, $intBT, '0', $arrOddsID[0], $arrOdds[0]);
	if($arrGDate["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $_strJoinNumberCheckOutInsert[$arrGDate["Err_Message"]];
		include("wager_error.php");
	}elseif($arrGDate["isMessage"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $_strJoinNumberCheckOutInsert[$arrGDate["Message"]];
		include("wager_error.php");
	}
	$flOdds = floatval($arrGDate["Odds"]);
	$PayOffMoney = $intStakeMoney * $flOdds;
	
	//取第二次
	$arrGDate_second = getWagerArrayData($intLID, $intLType, $intGID, $intBT, '1', $arrOddsID[1], $arrOdds[1]);
	if($arrGDate_second["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $_strJoinNumberCheckOutInsert[$arrGDate_second["Err_Message"]];
		include("wager_error.php");
	}elseif($arrGDate_second["isMessage"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $_strJoinNumberCheckOutInsert[$arrGDate_second["Message"]];
		include("wager_error.php");
	}
	$flOdds_second = floatval($arrGDate_second["Odds"]);
	$PayOffMoney_second	= $intStakeMoney * $flOdds_second;

} else {

	//取得最新號碼賠率資料(1個賠率)
	$arrGDate = getWagerArrayData($intLID, $intLType, $intGID, $intBT, $intNum, $_data["OddsID"], $_data["Odds"]);
	if($arrGDate["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $_strJoinNumberCheckOutInsert[$arrGDate["Err_Message"]];
		include("wager_error.php");
	}elseif($arrGDate["isMessage"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $_strJoinNumberCheckOutInsert[$arrGDate["Message"]];
		include("wager_error.php");
	}

	$flOdds 	= floatval($arrGDate["Odds"]);
	$PayOffMoney 	= $intStakeMoney * $flOdds;
}


//組合成功顯示可贏金額字串
eval("\$PayOffValue = \"$PayOffValue\";");
//eval("\$JoinDocTitle = \"$_strJoinNumberCheckOutInsert[JoinDocTitle]\";");

//組合陣列 for bet_BetListContent 用
$arrWCont["GameDate_x"] 	= $arrGDate["GameDate"];
$flMemPayOff 				= ($intStakeMoney*($flOdds - 1));
$arrWCont["GambleType"] 	= $objMemberInfo -> GambleType;
$arrWCont["MemAccount_x"] 	= $objMemberInfo -> Account;
$arrWCont["isBlack"] 		= $objMemberInfo -> isBlack;
$arrWCont["SuppleID_x"]		= $intSuppleWagerID."";

//組合陣列 for bet_BetList 用
if ($flOdds_second) {
	$arrWList["OddsID2"] 	= $arrGDate_second["OddsID"];
	$arrWList["Odds2"] 		= $flOdds_second;
}

$arrWList["LottoID"] 		= $arrGDate["LottoID"];
$arrWList["LType"] = $intLType;
$arrWList["Bet_Type"] 		= $arrGDate["Bet_Type"];
$arrWList["GambleID"] 		= $arrGDate["GambleID"];
$arrWList["Number"] 		= $intNumber;
$arrWList["OddsID"] 		= $arrGDate["OddsID"];
$arrWList["Odds"]			= $flOdds;
$arrWList["PayupDate_x"] 	= $arrGDate["PayupDate"];
$arrWList["MemPayOff"] 		= $flMemPayOff*$ChoiceGroup;
$arrWList["MemID"] 			= $objMemberInfo -> ID;
$arrWList["MemExchangeRate"] 	= $flMemERate;
$arrWList["MemCurrency"] 	= $objMemberInfo -> ERID;
$arrWList["MemStake"]		= $intStakeMoney*$ChoiceGroup;
$arrWList["MemCommission"] 	= 1 - ($arrComm[1] / 100);
$arrWList["SubID"] 			= $objMemberInfo -> SubID;
$arrWList["SubCommission"] 	= 1 - ($arrComm[2] / 100);
$arrWList["SubRate"] 		= $objMemberInfo -> SubScale;
$arrWList["MainID"] 		= $objMemberInfo -> MainID;
$arrWList["MainCommission"] 	= 1 - ($arrComm[3] / 100);
$arrWList["MainRate"]		= $objMemberInfo -> MainScale;
$arrWList["StockID"] 		= $objMemberInfo -> StockID;
$arrWList["StockCommission"] 	= 1 - ($arrComm[4] / 100);
$arrWList["StockRate"] 		= $objMemberInfo -> StockScale;
$arrWList["DirectorID"] 	= $objMemberInfo -> DirectorID;
$arrWList["DirectorCommission"] 	= 1 - ($arrComm[5] / 100);
$arrWList["DirectorRate"] 	= $objMemberInfo -> DirectorScale;
$arrWList["Layer6ID"] 	= $objMemberInfo -> Layer6ID;
$arrWList["Layer6Commission"] 	= 1 - ($arrComm[6] / 100);
$arrWList["Layer6Rate"] 	= $objMemberInfo -> Layer6Scale;
if($objMemberInfo -> WagerType >= 2)$arrWList["WagerType"] = $objMemberInfo -> WagerType;

if($arrWList["StockCommission"] < 0 || $arrWList["StockCommission"] >= 0.2 || $arrWList["StockCommission"] < $arrWList["MainCommission"] || $arrWList["MainCommission"] < $arrWList["SubCommission"] || $arrWList["SubCommission"] < $arrWList["MemCommission"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = "Commission Error!!";
	include("wager_error.php");
}

//$MaxEventLimit = $EventLimit - 本次下注...
$MaxEventLimit = $EventLimit - $intStakeMoney;

//取得此會員此場已下注號碼的累積金額
$arrParlaysNumberEven = get_ParlaysNumberEven($objMemberInfo -> ID, $intGID, $intBT, $MaxEventLimit);
if($arrParlaysNumberEven["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime 		= Getmicrotime ();
	$RunTime 		= 'RunTime:'. ($endTime - $startTime);
	$Err_Message 		= $_strJoinNumberCheckOutInsert[$arrParlaysNumberEven["ErrorMessage"]];
	$Err_HiddenMessage 	= $arrParlaysNumberEven["ErrorHiddenMessage"];
	include("wager_error.php");

} elseif ($arrParlaysNumberEven["Number"] != '0') {
	//比對此會員此場已下注號碼的累積金額是否已超過單場限額
	foreach ($JoinArray as $value) {
		$Join_Value	= null;
		$Number_Value	= null;
		$JoinNumber	= explode('.', $value);

		//sort($JoinNumber);

		foreach ($JoinNumber as $arrValue) {
			$Number_Value .= $arrValue;
			$Join_Value .= $arrValue . $_strJoinNumberCheckOutInsert[OffItemsSeries];
		}

		$Number_Value = str_repeat("0", (12 - strlen($Number_Value))) . $Number_Value;

		if (isset($arrParlaysNumberEven[$Number_Value])) {
			$JoinNumberValue .= substr($Join_Value, 0, -2) . '<br>';
		}
	}
	
	if ($JoinNumberValue) {
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		eval("\$Err_Message = \"$_strJoinNumberCheckOutInsert[OverEvenLimit]\";");
		$endTime 		= Getmicrotime ();
		$RunTime 		= 'RunTime:'. ($endTime - $startTime);
		$Err_Title		= $_strJoinNumberCheckOutInsert["BetError"];
		$Err_HiddenMessage	= '<br>'. $_strJoinNumberCheckOutInsert["EvenLimits"]."(".number_format($EventLimit).")<br>";
		$Err_HiddenMessage	.= $_strJoinNumberCheckOutInsert["ThisStake"]."(".number_format($arrParlaysNumberEven[$Number_Value] + $intStakeMoney).")";
		$Err_HiddenMessage	.= "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
}


//$MaxEventLimitGap = 最高差額 - 本次下注...
$JoinNumberValue  = null;
if($_KSysParam["isRealJumpWager"] == 0){
	$MaxEventLimitGap = $_arrBetParam[$intBT][$intAN]["MaxStakeGap"] - $intStakeMoney;
}else{
	$MaxEventLimitGap = $_arrBetParam[$intBT][$intAN]["MaxStakeGap"] - $intStakeMoney*$objMemberInfo -> DirectorScale;
}
//$MaxEventLimitGap = $_arrBetParam[$intBT][$intAN][MaxStakeGap] - $intStakeMoney;

//取得此 Bet_Type 的累積差額
$arrParlaysNumberEvenGap = get_ParlaysNumberEvenGap ($intGID, $intBT, $JoinArray);
if($arrParlaysNumberEvenGap["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime 		= Getmicrotime ();
	$RunTime 		= 'RunTime:'. ($endTime - $startTime);
	$Err_Message 		= $_strJoinNumberCheckOutInsert[$arrParlaysNumberEvenGap["ErrorMessage"]];
	$Err_HiddenMessage 	= $arrParlaysNumberEvenGap["ErrorHiddenMessage"];
	include("wager_error.php");
} elseif ($arrParlaysNumberEvenGap["Number"] != '0') {
	//比對此會員此場已下注號碼的累積金額是否已超過單場限額
	foreach ($JoinArray as $value) {
		$Join_Value	= null;
		$Number_Value	= null;
		$JoinNumber	= explode('.', $value);

		//sort($JoinNumber);

		foreach ($JoinNumber as $arrValue) {
			$Number_Value .= $arrValue;
			$Join_Value .= $arrValue . $_strJoinNumberCheckOutInsert[OffItemsSeries];
		}

		$Number_Value = str_repeat("0", (12 - strlen($Number_Value))) . $Number_Value;

		//資料庫有值有值而且超過最高差額 or 資料庫無值但是下注金額已超過最高差額
		if ((isset($arrParlaysNumberEvenGap[$Number_Value]) && ($arrParlaysNumberEvenGap[$Number_Value] > $MaxEventLimitGap)) || (!isset($arrParlaysNumberEvenGap[$Number_Value]) && ($intStakeMoney > $MaxEventLimitGap))) {
			$JoinNumberValue .= substr($Join_Value, 0, -2) . '<br>';
		}
	}

	if ($JoinNumberValue) {
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		eval("\$Err_Message = \"$_strJoinNumberCheckOutInsert[OverEvenGapLimit]\";");
		$endTime 		= Getmicrotime ();
		$RunTime 		= 'RunTime:'. ($endTime - $startTime);
		$Err_Title		= $_strJoinNumberCheckOutInsert["BetError"];
		$Err_HiddenMessage	= '<br>'. $_strJoinNumberCheckOutInsert["EvenGapLimits"]."(".number_format($EventLimit).")<br>";
		$Err_HiddenMessage	.= $_strJoinNumberCheckOutInsert["ThisStake"]."(".number_format($arrParlaysNumberEvenGap[$Number_Value] + $intStakeMoney).")";
		$Err_HiddenMessage	.= "\nLine:".__LINE__.",\nPage:".__FILE__;
		include("wager_error.php");
	}
}

//組合陣列 for bet_BetListContent 用
$arrWCont["checkkey_x"]	= str_replace("0.", $_data["CheckKey2"]."-", $_data["CheckKey1"]);

if ($arrOddsID[1]) {
	//取得最新號碼賠率資料 (2個賠率)
	$arrWCont["Content_x"] = getWagerConditions($intLType, $arrGDate["Bet_Type"], $intNumber, $flOdds, $arrGDate["SerialNo"], $flOdds_second, $arrAllJNumber, $ChoiceGroup);
} else {
	//取得最新號碼賠率資料(1個賠率)
	$arrWCont["Content_x"] = getWagerConditions($intLType, $arrGDate["Bet_Type"], $intNumber, $flOdds, $arrGDate["SerialNo"], '', $arrAllJNumber, $ChoiceGroup);
}
$arrWCont["OneStake"] = $intStakeMoney;
$arrWCont["JoinNumber_x"] = $strJoinNumber;

//寫入注單
$arrBetData = InsertWagerToDB($arrWCont, $arrWList);
if($arrBetData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_strJoinNumberCheckOutInsert[$arrBetData["ErrorMessage"]];
	$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"];
	include("wager_error.php");
}
$intWagerID = $arrBetData["BetID"];
$JoinDocTitle = $_strJoinNumberCheckOutInsert[WagerNo].$intWagerID;

//寫入下注號碼 ParlaysNumberEven ... 是 Even 喔
$arrBetParlaysEvenData = InsertParlaysNumberEvenToDB($arrWList["MemID"], $arrWList["SubID"], $arrWList["MainID"], $arrWList["StockID"], $arrWList["DirectorID"], $arrWList["Layer6ID"], $arrWList["GambleID"], $arrWList["Bet_Type"], $intWagerID, $JoinArray, $intStakeMoney, $arrWList["Odds"], $arrWList["Odds2"]);
if($arrBetParlaysEvenData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $_strJoinNumberCheckOutInsert[$arrBetParlaysEvenData["ErrorMessage"]];
	$Err_HiddenMessage = $arrBetParlaysEvenData["ErrorHiddenMessage"];
	include("wager_error.php");
}

$arrRealToday = array();
$arrRealToday[$objMemberInfo -> SubID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
								"C" => $arrWList["MemStake"]*$arrWList["MemCommission"]*$flMemERate*(1 - $objMemberInfo -> SubScale), 
								"RS" => $arrWList["MemStake"]*$flMemERate*(1 - $objMemberInfo -> SubScale), 
								"RP" => $arrWList["MemPayOff"]*$flMemERate*(1 - $objMemberInfo -> SubScale));

$arrRealToday[$objMemberInfo -> MainID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
								"C" => $arrWList["MemStake"]*$arrWList["SubCommission"]*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale), 
								"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale), 
								"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> SubScale - $objMemberInfo -> MainScale));

$arrRealToday[$objMemberInfo -> StockID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
								"C" => $arrWList["MemStake"]*$arrWList["MainCommission"]*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale), 
								"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale), 
								"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> MainScale - $objMemberInfo -> StockScale));

$arrRealToday[$objMemberInfo -> DirectorID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
								"C" => $arrWList["MemStake"]*$arrWList["StockCommission"]*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale), 
								"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale), 
								"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> StockScale - $objMemberInfo -> DirectorScale));

$arrRealToday[$objMemberInfo -> Layer6ID] = array("W" => 1, "S" => $arrWList["MemStake"], "P" => $arrWList["MemPayOff"], 
								"C" => $arrWList["MemStake"]*$arrWList["DirectorCommission"]*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale), 
								"RS" => $arrWList["MemStake"]*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale), 
								"RP" => $arrWList["MemPayOff"]*$flMemERate*($objMemberInfo -> DirectorScale - $objMemberInfo -> Layer6Scale));

//自動補貨
$arrPlusStake = array("Stake" => 0, "RealStake" => 0, "PayOff" => 0, "RealPayOff" => 0);
$j = 0;
$intNumber = 0;
foreach($arrAutoSuppleGroup as $strNum => $arrAutoSG){
	foreach($arrAutoSG as $intUID => $intGroup){
		/*$strJN1 = substr($strNum, 1, 2);
		$strJN2 = substr($strNum, 3, 2);
		$strJN3 = substr($strNum, 5, 2);
		if($strJN1 == '00'){
			$strJoinNumber = $strJN2.','.$strJN3;
			$arrJoinNumber = array(1 => $strJN2, 2 => $strJN3);
		}else{
			$strJoinNumber = $strJN1.','.$strJN2.','.$strJN3;
			$arrJoinNumber = array(1 => $strJN1, 2 => $strJN2, 3 => $strJN3);
		}*/
		$arrJoinNumber = array();
		for($i = ((6 - $GroupCounter)*2 + 1); $i < 12; $i += 2){
			$strN = substr($strNum, $i, 2);
			$arrJoinNumber[] = $strN;
		}
		$strJoinNumber = implode(",", $arrJoinNumber);


		$arrWListTmp = $arrWList;
		$arrWListTmp["MemExchangeRate"] = 1;
		$arrWListTmp["MemCurrency"] = 1;
		$arrWListTmp["Number"] = 0;
		$arrWListTmp["MemStake"] = $arrAutoSuppleGold[$strNum][$intUID];
		$arrWListTmp["MemPayOff"] = $arrAutoSuppleGold[$strNum][$intUID]*($arrWListTmp["Odds"] - 1);
		$arrWListTmp["WagerType"] = $intGroup;

		$arrWContTmp = $arrWCont;
		if ($arrOddsID[1]) {
			//取得最新號碼賠率資料 (2個賠率)
			$arrWContTmp["Content_x"] = getWagerConditions($intLType, $arrGDate["Bet_Type"], $intNumber, $flOdds, $arrGDate["SerialNo"], $flOdds_second, $arrJoinNumber, 1);
		} else {
			//取得最新號碼賠率資料(1個賠率)
			$arrWContTmp["Content_x"] = getWagerConditions($intLType, $arrGDate["Bet_Type"], $intNumber, $flOdds, $arrGDate["SerialNo"], '', $arrJoinNumber, 1);
		}

		$strCheckKey = str_replace("0.", $_data["CheckKey2"].$j.$intGroup.$intBT.$intNum."-", $_data["CheckKey1"]);
		$arrWContTmp["checkkey_x"] = $strCheckKey;
		$arrWContTmp["Content_x"] = $arrWContTmp["Content_x"]." (Auto)";
		$arrWContTmp["JoinNumber_x"] = $strJoinNumber;
		$arrWContTmp["OneStake"] = $arrAutoSuppleGold[$strNum][$intUID];

		$arrWListTmp["Layer6Commission"] = $arrCommission[$objMemberInfo -> Layer6ID];
		$arrWListTmp["DirectorCommission"] = $arrCommission[$objMemberInfo -> DirectorID];
		$arrWListTmp["StockCommission"] = $arrCommission[$objMemberInfo -> StockID];
		$arrWListTmp["MainCommission"] = $arrCommission[$objMemberInfo -> MainID];
		$arrWListTmp["SubCommission"] = $arrCommission[$objMemberInfo -> SubID];

		$arrLayerScale = $objMemberInfo -> LayerScale[$intGroup];
		$arrWListTmp["SubRate"] = $arrLayerScale[2];
		$arrWListTmp["MainRate"] = $arrLayerScale[3];
		$arrWListTmp["StockRate"] = $arrLayerScale[4];
		$arrWListTmp["DirectorRate"] = $arrLayerScale[5];
		$arrWListTmp["Layer6Rate"] = $arrLayerScale[6];
		switch($intGroup){
			case 6:
				$arrWListTmp["DirectorID"] = $intUID;
				$arrWListTmp["DirectorCommission"] = $arrCommission[$intUID];

			case 5:
				$arrWListTmp["StockID"] = $intUID;
				$arrWListTmp["StockCommission"] = $arrCommission[$intUID];
				//$arrWListTmp["DirectorRate"] = 1;
			case 4:
				$arrWListTmp["MainID"] = $intUID;
				$arrWListTmp["MainCommission"] = $arrCommission[$intUID];
				//$arrWListTmp["StockRate"] = 1;
			case 3:
				$arrWListTmp["SubID"] = $intUID;
				$arrWListTmp["SubCommission"] = $arrCommission[$intUID];;
				//$arrWListTmp["MainRate"] = 1;
			case 2:
				$arrWListTmp["MemID"] = $intUID;
				$arrWListTmp["MemCommission"] = $arrCommission[$intUID];
				//$arrWListTmp["SubRate"] = 1;
				break;
		}
		$arrWContTmp["MemAccount_x"] = $objMemberInfo -> LayerAcc[$intGroup];

		switch($intGroup){
			case 2:
				$arrRealToday[$objMemberInfo -> MainID]["W"] += 1;
				$arrRealToday[$objMemberInfo -> MainID]["S"] += $arrWListTmp["MemStake"];
				$arrRealToday[$objMemberInfo -> MainID]["P"] += $arrWListTmp["MemPayOff"];
				$arrRealToday[$objMemberInfo -> MainID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["SubCommission"]*($arrLayerScale[2] - $arrLayerScale[3]);
				$arrRealToday[$objMemberInfo -> MainID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[2] - $arrLayerScale[3]);
				$arrRealToday[$objMemberInfo -> MainID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[2] - $arrLayerScale[3]);
			case 3:
				$arrRealToday[$objMemberInfo -> StockID]["W"] += 1;
				$arrRealToday[$objMemberInfo -> StockID]["S"] += $arrWListTmp["MemStake"];
				$arrRealToday[$objMemberInfo -> StockID]["P"] += $arrWListTmp["MemPayOff"];
				$arrRealToday[$objMemberInfo -> StockID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["MainCommission"]*($arrLayerScale[3] - $arrLayerScale[4]);
				$arrRealToday[$objMemberInfo -> StockID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[3] - $arrLayerScale[4]);
				$arrRealToday[$objMemberInfo -> StockID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[3] - $arrLayerScale[4]);
			case 4:
				$arrRealToday[$objMemberInfo -> DirectorID]["W"] += 1;
				$arrRealToday[$objMemberInfo -> DirectorID]["S"] += $arrWListTmp["MemStake"];
				$arrRealToday[$objMemberInfo -> DirectorID]["P"] += $arrWListTmp["MemPayOff"];
				$arrRealToday[$objMemberInfo -> DirectorID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["StockCommission"]*($arrLayerScale[4] - $arrLayerScale[5]);
				$arrRealToday[$objMemberInfo -> DirectorID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[4] - $arrLayerScale[5]);
				$arrRealToday[$objMemberInfo -> DirectorID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[4] - $arrLayerScale[5]);
			case 5:
				$arrRealToday[$objMemberInfo -> Layer6ID]["W"] += 1;
				$arrRealToday[$objMemberInfo -> Layer6ID]["S"] += $arrWListTmp["MemStake"];
				$arrRealToday[$objMemberInfo -> Layer6ID]["P"] += $arrWListTmp["MemPayOff"];
				$arrRealToday[$objMemberInfo -> Layer6ID]["C"] += $arrWListTmp["MemStake"]*$arrWListTmp["StockCommission"]*($arrLayerScale[5] - $arrLayerScale[6]);
				$arrRealToday[$objMemberInfo -> Layer6ID]["RS"] += $arrWListTmp["MemStake"]*($arrLayerScale[5] - $arrLayerScale[6]);
				$arrRealToday[$objMemberInfo -> Layer6ID]["RP"] += $arrWListTmp["MemPayOff"]*($arrLayerScale[5] - $arrLayerScale[6]);
			case 6:
				break;
		}
		$arrRealToday[$intUID]["SW"] += 1;
		$arrRealToday[$intUID]["SS"] += $arrWListTmp["MemStake"];
		$arrRealToday[$intUID]["SP"] += $arrWListTmp["MemPayOff"];
		$arrRealToday[$intUID]["SC"] += $arrWListTmp["MemCommission"]*$arrWListTmp["MemStake"];

		$arrPlusStake["Stake"] +=  $arrWListTmp["MemStake"];
		$arrPlusStake["RealStake"] +=  $arrWListTmp["MemStake"]*$arrWListTmp["DirectorRate"];
		$arrPlusStake["PayOff"] +=  $arrWListTmp["MemPayOff"];
		$arrPlusStake["RealPayOff"] +=  $arrWListTmp["MemPayOff"]*$arrWListTmp["DirectorRate"];

		//寫入自動補貨注單
		$arrAutoSuppleBetData = InsertWagerToDB($arrWContTmp, $arrWListTmp);
		if($arrAutoSuppleBetData["isError"]){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$Err_Message = $arrAutoSuppleBetData["ErrorMessage"]."1";
			$Err_HiddenMessage = $arrAutoSuppleBetData["ErrorHiddenMessage"];
			include("wager_error.php");
		}
		$j++;
		$intAutoSuppleWagerID = $arrAutoSuppleBetData["BetID"];

		//寫入下注號碼 ParlaysNumberEven ... 是 Even 喔
		$arrBetParlaysEvenData = InsertParlaysNumberEvenToDB($arrWList["MemID"], $arrWList["SubID"], $arrWList["MainID"], $arrWList["StockID"], $arrWList["DirectorID"], $arrWList["Layer6ID"], $arrWList["GambleID"], $arrWList["Bet_Type"], $intAutoSuppleWagerID, array(implode(".", $arrJoinNumber)), $arrWListTmp["MemStake"], $arrWList["Odds"], $arrWList["Odds2"]);
		if($arrBetParlaysEvenData["isError"]){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$endTime = Getmicrotime ();
			$RunTime = 'RunTime:'. ($endTime - $startTime);
			$Err_Message = $_strJoinNumberCheckOutInsert[$arrBetParlaysEvenData["ErrorMessage"]];
			$Err_HiddenMessage = $arrBetParlaysEvenData["ErrorHiddenMessage"];
			include("wager_error.php");
		}
	}
}



//下單計數
$StartCounter = $ChoiceGroup;
//押碼跳動 - 更新 TodayGame 壓碼不會跳動
$arrJumpData = WagerJump($arrGDate, $intStakeMoney*$ChoiceGroup, $arrWList["MemPayOff"], $_arrBetParam[$arrGDate["Bet_Type"]][$intAN], $_arrGambleTypeAllOdds[$arrGDate["Bet_Type"]][0], $arrWList["MemExchangeRate"]*$arrWList["DirectorRate"], $arrWList["DirectorCommission"], $arrPlusStake);
if($arrJumpData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $arrBetData["ErrorMessage"].$GLOBALS["errormessage"];
	$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"].$GLOBALS["errorhidden"];
	include("wager_error.php");
}

if(!setCountWagerForAllLayers($intLID, $intGID, array($intBT => array($arrRealToday)))){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$Err_Message = ($_strWagerInsert[$GLOBALS["errormessage"]] != "")?$_strWagerInsert[$GLOBALS["errormessage"]]:$GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"]."6";
	include("wager_error.php");
}

foreach($JoinArray as $value) {
	//列表用下注號碼
	$arrSucc[JoinNumber][] 		= $value;

}
/*$StartCounter = 0;
foreach($JoinArray as $value) {
	//列表用下注號碼
	$arrSucc[JoinNumber][] 		= $value;

	//押碼跳動 - 更新 TodayGame 壓碼不會跳動
	$arrJumpData = WagerJump($arrGDate, $intStakeMoney, $arrWList["MemPayOff"], $_arrBetParam[$arrGDate["Bet_Type"]][$intAN], $_arrGambleTypeAllOdds[$arrGDate["Bet_Type"]][0], $arrWList["MemExchangeRate"]*$arrWList["DirectorRate"], $arrWList["DirectorCommission"], $arrPlusStake);
	if($arrJumpData["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$endTime = Getmicrotime ();
		$RunTime = 'RunTime:'. ($endTime - $startTime);
		$Err_Message = $arrBetData["ErrorMessage"].$GLOBALS["errormessage"];
		$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"].$GLOBALS["errorhidden"];
		include("wager_error.php");
	}
	$StartCounter++;
}*/

//產生累積注額陣列
if(!getWagerArrayGold($objMemberInfo -> ID)){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$endTime = Getmicrotime ();
	$RunTime = 'RunTime:'. ($endTime - $startTime);
	$Err_Message = $GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}

//產生下注列表
for ($LoopTime = 0; $LoopTime < $StartCounter; $LoopTime++) {
	$JoinNumberList .= '
	  			<tr class=left_jn_pg_choice_no>
	    			  <td class=left_jn_pg_choice_id>'. (string)($LoopTime + 1) .'</td>
	    			  <td class=left_jn_pg_choice_no>'. $arrSucc[JoinNumber][$LoopTime] .'</td>
	    			  <td class=left_jn_pg_choice_cash>'. $intStakeMoney .'</td>
	  			</tr>
	  		    '."\n";//$arrSucc[BetID][$LoopTime]
}

//更新剩餘信用額度
include (__Root_Path."/tmp/credit-".$objMemberInfo -> ID.".php");
$CreditSurplus	= $objMemberInfo->CreditLine - $_arrMemStake["_SUM"];

//交易完成
PMA_mysql_query("COMMIT", $_ConnMaster);
AddData_Log($objMemberInfo -> ID,"下注成功 ", $arrBetData["LogData"]);
$Err_Message = "Success!!";
@mysql_close($$_Conn);
include("joinnumbercheckout_success.php");
?>